package com.jnu.homeworkapplication;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private List<Book> Books;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initData();
        
        RecyclerView recyclerView = findViewById(R.id.recycle_view_books);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setAdapter(new MyRecycleViewAdapter(Books));


    }

    private void initData() {
        Books = new ArrayList<Book>();
        Books.add(new Book("软件项目管理案例教程（第4版）", R.drawable.book_2));
        Books.add(new Book("创新工程实践", R.drawable.book_no_name));
        Books.add(new Book("信息安全数学基础（第2版）", R.drawable.book_1));
    }

    private class MyRecycleViewAdapter extends RecyclerView.Adapter{

        private List<Book> Books;

        public MyRecycleViewAdapter(List<Book> Books){
            this.Books = Books;
        }

        @NonNull
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

            View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.book_item,parent,false);
            ViewHolder viewHolder = new ViewHolder(itemView);

            return viewHolder;
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
            ViewHolder Holder = (ViewHolder)holder;
            Holder.getImageView().setImageResource(Books.get(position).getCoverResourceId());
            Holder.getTextView().setText(Books.get(position).getTitle());


        }

        @Override
        public int getItemCount() {
            return Books.size();
        }

        private class ViewHolder extends RecyclerView.ViewHolder{
            private final ImageView imageView;
            private final TextView textView;
            public ViewHolder(View itemView) {
                super(itemView);
                this.imageView = findViewById(R.id.image_view_book_cover);
                this.textView = findViewById(R.id.text_view_book_title);
            }

            public ImageView getImageView(){
                return imageView;
            }

            public TextView getTextView(){
                return textView;
            }
        }
    }
}

